package com.stephen.redisdemo.atomic;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: stephen
 * @create: 2020-02-29 17:38
 * @description: 生产者消费者
 * 使用 lock ,await ,signalAll
 * 以前是使用 synchronized,wait,notify
 * 注意,wait和nofity都是Object类中的方法
 * 所有的多线程判断,都必须用while
 * 两个线程,用if可以,但是多于2个线程,用if就会出问题
 * 会出现虚假唤醒的情况
 */
class MyData {

    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    /**
     * 生产
     * @throws InterruptedException
     */
    public void increment() throws InterruptedException {
        lock.lock();
        try {
            // 1. 判断
            while (number != 0) {
                // 等待,不能生产
                condition.await();
            }
            // 2. 干活
            number++;
            System.out.println(Thread.currentThread().getName() + "\t" + number);
            // 3. 唤醒其它线程
            condition.signalAll();
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            lock.unlock();
        }
    }

    /**
     * 消费
     * @throws InterruptedException
     */
    public void decrement() throws InterruptedException {
        lock.lock();
        try {
            // 1. 判断
            while (number == 0) {
                // 等待,不能消费
                condition.await();
            }
            // 2. 干活
            number--;
            System.out.println(Thread.currentThread().getName()+"\t" + number);
            // 3. 唤醒其它线程
            condition.signalAll();
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            lock.unlock();
        }
    }

}

public class Product_Consumer {

    public static void main(String[] args) {
        MyData myData = new MyData();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    myData.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t1").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    myData.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t2").start();
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    myData.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t3").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    myData.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t4").start();
    }
}
